package cn.itcast.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequiredArgsConstructor
@RequestMapping("/ai")
public class ChatController {

    private final ChatClient chatClient;


    /**
     * 处理流式聊天请求
     * @return 包含逐条聊天响应的响应式数据流
     */
    @PostMapping(value = "stream", produces = "text/html;charset=utf-8")
    public Flux<String> chatStream(String prompt,String chatId) {
        // 调用聊天客户端生成流式响应内容
        return this.chatClient.prompt()
                .user(prompt)
                .stream()
                .content()
                // 在流结束时添加结束标记
                .concatWith(Flux.just("[END]"));
    }


    /**
     * 处理流式聊天请求，返回服务器发送事件（SSE）格式的响应流
     *
     * @return 包含逐条聊天响应的响应式数据流，通过Server-Sent Events协议传输
     */
//    @PostMapping(value = "stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
//    public Flux<String> chatStream(String prompt,String chatId) {
//        return this.chatClient.prompt()
//                .user(prompt)
//                .stream()
//                .content()
//                // 在流结束时添加结束标记
//                .concatWith(Flux.just("[END]"));
//    }
}
